import pytest
import requests
import random,allure
from faker import Faker

faker = Faker(locale='zh_cn')
project_url = "http://60.205.190.66:8899"


@allure.feature("用户分组")
class Test_CaseSet():
    name = faker.name()


    @pytest.fixture(scope='function', autouse=True)
    def setup_function(self, request):
        response = requests.post(url=project_url + "/adminapi/login",
                                 json={"account": "admin", "pwd": "920509", "imgcode": "0"})
        self.token = response.json()['data']['token']
        print('开始')
        yield
        print('退出')
    @pytest.mark.skipif(condition=1,reason="分组不用测这条")
    @allure.story("添加分组")
    def test_a(self):  # 话术
        random_num = random.randint(1, 9)

        response1 = requests.post(project_url + "/adminapi/app/wechat/speechcraft",
                                  json={"cate_id": "", "title": "测试十七期", "message": str(self.name), "sort": 0},
                                  headers={"Authori-Zation": "Bearer " + self.token})
        assert response1.json()['msg'] == "创建话术成功"
        # 步骤一，先查
        response2 = requests.get(project_url + "/adminapi/app/wechat/speechcraft?page=1&limit=15&cate_id=",
                                 headers={"Authori-Zation": "Bearer " + self.token})
        for i in response2.json()['data']['list']:
            if i['message'] == str(self.name):
                self.id = i['id']
        #
        # 步骤二：修改
        response3 = requests.put(project_url + "/adminapi/app/wechat/speechcraft/" + str(self.id),
                                 json={"cate_id": 0, "title": "测试十七期", "message": str(faker.name()), "sort": 0},
                                 headers={"Authori-Zation": "Bearer " + self.token})
        assert response3.json()['msg'] == "修改成功"
        #
        # 删除
        response4 = requests.delete(project_url + "/adminapi/app/wechat/speechcraft/" + str(self.id),
                                    headers={"Authori-Zation": "Bearer " + self.token})
        assert response4.json()['msg'] == "删除成功"
    @pytest.mark.flaky(reruns = 3,reruns_delay = 1 )
    @allure.story("添加分组")
    @pytest.mark.qb
    def test_b(self):  # 分组
        random_num = random.randint(1, 9)

        response1 = requests.post(url=project_url + "/adminapi/user/user_group/save.html",
                                  json={"group_name": "小天才" + str(random_num)},
                                  headers={"Authori-Zation": "Bearer " + self.token})
        msg = response1.json()['msg']
        assert msg == "提交成功！"
        list_id = []

        # 修改分组 - 步骤1，拿到id
        response2 = requests.get(url=project_url + "/adminapi/user/user_group/list?page=1&limit=15",
                                 headers={"Authori-Zation": "Bearer " + self.token})

        for i in response2.json()['data']['list']:
            if i['group_name'] == "小天才" + str(random_num):
                list_id.append(i['id'])  # 拿到id
        # 修改分组 - 步骤2  传入id进行修改
        response3 = requests.post(project_url + "/adminapi/user/user_group/save.html",
                                  json={"id": list_id[0], "group_name": "小天才plus"},
                                  headers={"Authori-Zation": "Bearer " + self.token})
        assert response3.json()['msg'] == "提交成功！"
        #
        # 删除分组
        response4 = requests.delete(project_url + "/adminapi/user/user_group/del/" + str(list_id[0]),
                                    headers={"Authori-Zation": "Bearer " + self.token})
        assert response4.json()['msg'] == "删除成功!"
    @pytest.mark.skip
    @allure.story("添加分组")
    def test_c(self):
        random_num = random.randint(1, 9)
        response6 = requests.post(project_url + "/adminapi/product/category.html",
                                  headers={"Authori-Zation": "Bearer " + self.token},
                                  json={"pid": 0, "cate_name": self.name, "pic": "", "big_pic": "", "sort": 0,
                                        "is_show": 1})
        assert response6.json()['msg']=='添加分类成功!'
        response4 = requests.get(
            project_url + "/adminapi/product/category?pid=&is_show=&page=1&cate_name=" + str(self.name) + "&limit=15",
            headers={"Authori-Zation": "Bearer " + self.token})
        self.id = response4.json()['data']['list'][0]['id']

        response5 = requests.put(project_url+"/adminapi/product/category/"+str(self.id),headers={"Authori-Zation": "Bearer "+self.token},json={"pid": 0, "cate_name": self.name + str(random_num), "pic": "", "big_pic": "", "sort": 0, "is_show": 1})
        assert response5.json()['msg']=="修改成功!"
        # response6 = requests.delete(project_url + "/adminapi/product/category/" + str(self.id),
        #                             headers={"Authori-Zation": "Bearer " + self.token})
        # assert response6.json()['msg']=='删除成功!'


if __name__ == '__main__':
    pytest.main()
